package com.fuli.app.service.Serviceimpl;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.Pagination;
import com.fuli.app.common.SystemContext;
import com.fuli.app.dao.SignInDao;
import com.fuli.app.dao.UserDao;
import com.fuli.app.dto.SignInDTO;
import com.fuli.app.dto.query.SignInQueryDTO;
import com.fuli.app.entity.SignIn;
import com.fuli.app.entity.User;
import com.fuli.app.service.SignInService;
import com.fuli.app.service.UserService;
import com.fuli.app.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class SignInServiceImpl implements SignInService {
    @Autowired
    private SignInDao signInDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserServiceImpl userService;

    @Override
    public ActionResult<String> add(SignInDTO dto) {
        User user = userDao.get(dto.getUserId());
        if (user == null)
            return new ActionResult<>(-1, "用户不存在，签到失败");
        dto.setId(EntityUtils.uuid());
        //
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowDay = dateFormat.format(new Date());
        try {
            Date nowDate = dateFormat.parse(nowDay);
            dto.setCreateTime(nowDate.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SignIn po = signInDao.getByDate(dto.getUserId(), dto.getCreateTime());
        if (po == null) {
            signInDao.insert(dto);

            //更新用户的总积分
            userService.addSignInfo(dto);
            return new ActionResult<>(dto.getId());
        }
        return new ActionResult<>(-1, "签到失败，该用户今日已经签到过了");
    }

    @Override
    public ActionResult<String> delete(String id) {
        SignIn signIn = signInDao.get(id);
        if (signIn == null)
            return new ActionResult<>(id);
        signInDao.delete(id);
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<List<String>> beachDelete(List<String> ids) {
        signInDao.beachDelete(ids);
        return new ActionResult<>(ids);
    }

    @Override
    public ActionResult<SignInDTO> get(String id) {
        SignIn po = signInDao.get(id);
        SignInDTO signInDTO = EntityUtils.copyObject(po, SignInDTO.class);
        return new ActionResult<>(signInDTO);
    }

    @Override
    public ActionResult<Pagination<SignInDTO>> pageQuery(SignInQueryDTO dto) {
        Pagination pagination = new Pagination(dto.getPageNo(), dto.getPageSize());
        int count = signInDao.getCountByQuery(dto);
        pagination.setCount(count);
        if (count > 0) {
            List<SignIn> signIns = signInDao.pageQuery(dto);
            List<SignInDTO> signInDTOS = EntityUtils.copyList(signIns, SignInDTO.class);
            pagination.setRows(signInDTOS);
            return new ActionResult<>(pagination);
        }
        return new ActionResult<>(pagination);
    }
}
